package com.erdos.cas.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;

import com.erdos.cas.client.MesHttpClient;
import com.erdos.cas.entity.TokenVO;


public class AppListFrame extends JFrame implements ActionListener {
	
	/**
	 * 接受用户可访问的应用列表
	 */
	private List<TokenVO>  list;
	
	protected JButton exitButton;
	
	protected JButton hideButton;
	
	MesHttpClient hc ;
	
	AppListFrame t=this;
	
	protected TrayIcon tray;
	
	public AppListFrame(List<TokenVO> list){
		super("应用列表");
		this.list=list;
		hc=new MesHttpClient();
		init();
	}
	
	/**
	 * 界面布局的管理与初始化
	 */
	private void init(){
	
		JPanel buttons = new JPanel();
		buttons.setLayout(new GridLayout(1,0,3,3));
		Icon icon = new ImageIcon(AppListFrame.class.getResource("Duke.gif"));
		JPopupMenu menu = new JPopupMenu();
		JMenuItem ji=new JMenuItem("resume");
		menu.add(ji);
		menu.addSeparator();
		
		ji.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				t.setVisible(true);
			}
		});
		
		menu.addSeparator();
		JMenuItem exit = new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean flag = hc.logout(list.get(0).getLoginId());
				if(flag){
					System.exit(0);
				}else{
					System.exit(0);     //如果删除不成功也退出了
				}
			}
		});
		menu.add(exit);
		tray = new TrayIcon(icon, "My AppList", menu);
		tray.addActionListener(t);
		
		SystemTray.getDefaultSystemTray().addTrayIcon(tray);
		
		 this.setExtendedState( Frame.MAXIMIZED_BOTH );
		
		 Rectangle r=new Rectangle( 200,
				 150,
                500,
               350);
		 this .setMaximizedBounds(r);
		 this.setSize(500,300);
		this.setBounds(200,150,500,350);
		 Color blue = new Color(126,199,211);
		 this.setBackground(blue);
		 JPanel jp1=new JPanel();
		 JPanel jp2=new JPanel();
		
		 Font f=new Font("����",Font.BOLD,20);
		 JLabel jl=new JLabel("京唐MES系统登陆列表");
		 jl.setFont(f);
		
		 jp1.add(jl);
		 jp1.setBackground( blue);
		 jp1.setSize(500, 20);
		 jp1.setBounds(30, 0, 450, 30);
		  
		 jp2.setLayout(null);
		 jp2.setBackground(blue);
		 jp2.setSize(500,300);
		  
		
		 int location=10;
		 Iterator te=list.iterator();
		 while(te.hasNext()){
			 TokenVO token=(TokenVO) te.next();
			 JButton jb = new JButton(token.getName());
		     jb.addActionListener(this);
		    
	         jb.setSize(300,20);
		     Color green = new Color(199,237,215);
		     jb.setBackground(green);
		     jb.setBounds(20,location,450, 30);
		     location=jb.getY()+30;
		     jp2.add(jb);
		 }
		 
		 this.add(jp1, BorderLayout.NORTH);
		 this.add(jp2, BorderLayout.CENTER);
	
		 this.setVisible(true);
		 this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		 this.centerDialog(this);
	
	}

	/**
	 * 事件监听方法
	 */
	public void actionPerformed(ActionEvent e) {
	
		 Iterator te=list.iterator();
		 while(te.hasNext()){
			 TokenVO token=(TokenVO) te.next();
			
			 if(e.getActionCommand().equals(token.getName())){
					String cmd=token.getUrl();
					if (cmd.indexOf(".exe")!=-1){
						cmd=cmd+" -"+token.getLoginId()+","+token.getToken();
						
				}
			  if(cmd.substring(0, 4).equals("http")){
						cmd="/"+System.getenv("ProgramFiles")+"//Internet Explorer//IEXPLORE.EXE  -new  "
							+cmd+"?loginId="+token.getLoginId()+"&token="+token.getToken();
				}
					
			  if (cmd.indexOf(".jar")!=-1){
						cmd="java -jar "+System.getenv("CAS_HOME")+cmd+" "+token.getLoginId()+" "+token.getToken();
					}
					Runtime rt = Runtime.getRuntime(); 
					try {
						rt.exec(cmd);									//Process proc = rt.exec(cmd);
					  hc.loginApp(list.get(0).getLoginId(),e.getActionCommand());
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			
		 }
	
	}
	
	/**
	 * 初始化界面的位置
	 */
	public static void centerDialog(Window frame) {
		Dimension dialogSize = frame.getSize();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation(screenSize.width/2 - dialogSize.width/2,
		                  screenSize.height/2 - dialogSize.height/2);
	}  
}
